package com.zhiche.lisa.lspm.controller;

import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.service.statistic.StatisticService;
import com.zhiche.lisa.lspm.vo.statistic.StatisticVO;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/statistic")
@Api(value = "/statistic", description = "运力管理使用情况统计接口",tags={"运力管理使用情况统计接口"})
public class StatisticController {

    private static final Logger LOGGER = LoggerFactory.getLogger(StatisticController.class);

    @Autowired
    private StatisticService statisticService;

    @GetMapping(value = "/everyDay")
    @ApiOperation(value = "获取查询月份每日的统计数据", notes = "获取查询月份每日的统计数据",produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success",response = StatisticVO.class),@ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<StatisticVO>> getEveryDayStat(@ApiParam(name="yearMonth",value="查询年月",required=true,defaultValue = "2018-10") String yearMonth) {
        LOGGER.info("StatisticController.getEveryDayStat params:{}", yearMonth);
        RestfulResponse<List<StatisticVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            if(StringUtils.isNotEmpty(yearMonth)){
                String queryDate = yearMonth + "-01";
                List<StatisticVO> list = statisticService.getListStatics(queryDate);
                result.setData(list);
            }
        } catch (BaseException be) {
            LOGGER.error("StatisticController.getEveryDayStat error:{}", be.getMessage());
            result.setMessage(be.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("StatisticController.getEveryDayStat error:{}", e.getMessage());
            result.setMessage("操作失败");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}
